import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Tournament {

    private Map<String, Team> tournamentMap = new HashMap<>();

    private static int compare(Team t1, Team t2) {
        return t2.points - t1.points == 0 ? t1.name.compareTo(t2.name) : t2.points - t1.points;
    }

    String printTable() {
        StringBuilder result = new StringBuilder();
        result.append("Team                           | MP |  W |  D |  L |  P\n");

        List<Team> listTeam = new ArrayList<>();
        listTeam.addAll(this.tournamentMap.values());
        listTeam.sort(Tournament::compare);
        listTeam.forEach((set) -> result.append(set.toString()));

        return result.toString();
    }

    void applyResults(String input) {
        String[] sentences = input.split("\n");
        for (String ignored : sentences) {
            String[] tournamentResult = ignored.split(";");
            for (int i = 0; i < 2; i++) {
                if (!this.tournamentMap.containsKey(tournamentResult[i])) {
                    this.tournamentMap.put(tournamentResult[i], new Team(tournamentResult[i]));
                }
            }

            switch (tournamentResult[2]) {
                case "win":
                    this.tournamentMap.get(tournamentResult[0]).wonMatches();
                    this.tournamentMap.get(tournamentResult[1]).lostMatches();
                    break;
                case "draw":
                    this.tournamentMap.get(tournamentResult[0]).tiedMatches();
                    this.tournamentMap.get(tournamentResult[1]).tiedMatches();
                    break;
                case "loss":
                    this.tournamentMap.get(tournamentResult[0]).lostMatches();
                    this.tournamentMap.get(tournamentResult[1]).wonMatches();
                    break;
                default:
                    break;
            }
        }
    }

    class Team {

        String name;
        Integer played = 0;
        Integer won = 0;
        Integer lost = 0;
        Integer tied = 0;
        Integer points = 0;

        Team(String name) {
            this.name = name;
        }

        void wonMatches() {
            this.played++;
            this.won++;
            this.points += 3;
        }

        void tiedMatches() {
            this.played++;
            this.tied++;
            this.points++;
        }

        void lostMatches() {
            this.played++;
            this.lost++;
        }

        public String toString() {
            return String.format("%-31s|%3s |%3s |%3s |%3s |%3s\n",
                    this.name,
                    this.played,
                    this.won,
                    this.tied,
                    this.lost,
                    this.points);
        }
    }
}